<?php
class Comments
{
	protected $_table = null;
	public function __construct()
	{
		$this->_table = new DbTable_Comments();
	}
	public function getAdapter()
	{
		return $this->_table->getAdapter();
	}
	/**
	 * Create A Record
	 *
	 * @param
	 *       	 rowSet
	 * @return boolean
	 */
	public function add($row)
	{
		return $this->_table->insert($row);
	}
	
	/**
	 * Update One Row
	 *
	 * @param $row rowSet       	
	 * @param $order_id int       	
	 * @return boolean
	 */
	public function update($row, $pri)
	{
		$where = $this->_table->getAdapter()->quoteInto("comment_ID= ?", $pri);
		return $this->_table->update($row, $where);
	}
	
	public function delete($pri)
	{
		$where = $this->_table->getAdapter()->quoteInto("comment_ID= ?", $pri);
		return $this->_table->delete($where);
	}
	
	public function getByField($value, $field = 'comment_ID', $colums = "*")
	{
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table, $colums);
		$select->where("{$field} = ?", $value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	public function getAll()
	{
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table, "*");
		return $this->_table->getAdapter()->fetchAll($select);
	}
	public function getByCondition($condition = array(), $type = '*', $orderby = "", $pageSize = 0, $page = 1)
	{
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table, $type);
		$select->where("1 =?", 1);
		
		if ('count(*)' == $type)
		{
			return $this->_table->getAdapter()->fetchOne($select);
		} else
		{
			$select->order($orderby);
			if ($pageSize > 0 and $page > 0)
			{
				$start = ($page - 1) * $pageSize;
				$select->limit($pageSize, $start);
			}
			$sql = $select->__toString();
			$result = $this->_table->getAdapter()->fetchAll($sql);
			
			return $result;
		}
	}
}